import { defineStore } from "pinia"
import { UserState } from "@/stores/interface"
import piniaPersistConfig from "@/stores/helper/persist"
import { LoginSpace } from "@/api/interface/login"

export const useUserStore = defineStore({
  id: "UserStore",
  state: (): UserState => ({
    token: "",
    passwordToken: "",
    userInfo: { email: "", imgPath: "", name: "", phone: "", status: 0, roleList: [], deptList: [] },
    menus: [],
    perms: []
  }),
  getters: {},
  actions: {
    // 保存 菜单权限
    saveMenus(menus: LoginSpace.UserMenu[]) {
      this.menus = menus
    },
    // 保存 按钮权限
    savePerms(perms: string[]) {
      this.perms = perms
    },
    // 设置 Token
    setToken(token: string) {
      this.token = token
    },
    // 设置 首次登录修改密码Token
    setPasswordToken(token: string) {
      this.passwordToken = token
    },
    // 设置 用户信息
    setUserInfo(info: UserState["userInfo"]) {
      this.userInfo = info
    }
  },
  persist: piniaPersistConfig("UserStore")
})
